package cn.com.member.controller.discover;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ibm.icu.util.Calendar;

import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_discover;
import cn.com.api.entity.ApiU_discover_cate;
import cn.com.api.entity.ApiU_discover_spec;
import cn.com.api.service.ApiU_discoverService;
import cn.com.api.service.ApiU_discover_cateService;
import cn.com.api.service.ApiU_discover_specService;
import cn.com.member.controller.MemberMainController;
import cn.com.util.JsonUtil;
import cn.com.util.PageInfo;
import cn.com.util.Utils;

@Controller
@RequestMapping
public class DiscoverController {
	
	private static Logger logger = Logger.getLogger(MemberMainController.class);
	
	@Autowired
	private ApiU_discoverService apiU_discoverService;
	@Autowired
	private ApiU_discover_cateService apiU_discover_cateService;
	@Autowired
	private ApiU_discover_specService apiU_discover_specService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping("activity_discover-list")
	public ModelAndView activity_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("/discover/discover-activity-list");
		
		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(0);
		Map conditions = new HashMap();
		
		String title = request.getParameter("activity_name");
		String cateId = request.getParameter("cateId");
		String status = request.getParameter("status");
		
		Object webuser = request.getSession().getAttribute("webuser");
		
		if (webuser == null) {
			return null;
		} else {
			ApiTmember apiTmember = (ApiTmember) webuser;
			conditions.put("user_id", apiTmember.getCid().toString());
		}
		
		if (cateId != null && !cateId.equalsIgnoreCase("-1")) {
			conditions.put("cate_id", cateId);
		}
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", title);
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "0");
		
		List<Map<String,Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		mv.addObject("cates", cates);
		mv.addObject("u_discovers", u_discovers);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		
		return mv;
	}
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping("service_discover-list")
	public ModelAndView service_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("/discover/discover-service-list");
		
		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(1);
		Map conditions = new HashMap();
		
		String title = request.getParameter("activity_name");
		String cateId = request.getParameter("cateId");
		String status = request.getParameter("status");
		
		Object webuser = request.getSession().getAttribute("webuser");
		
		if (webuser == null) {
			return null;
		} else {
			ApiTmember apiTmember = (ApiTmember) webuser;
			conditions.put("user_id", apiTmember.getCid().toString());
		}
		
		if (cateId != null && !cateId.equalsIgnoreCase("-1")) {
			conditions.put("cate_id", cateId);
		}
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", title);
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "1");
		
		List<Map<String,Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		mv.addObject("cates", cates);
		mv.addObject("u_discovers", u_discovers);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		
		return mv;
	}
	
	@RequestMapping("create-activity-discover")
	public ModelAndView create_activity_discover(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("/discover/create-discover-activity");

		if(request.getParameter("id") != null){
			Integer id = Integer.valueOf(request.getParameter("id"));
			ApiU_discover u_discover = apiU_discoverService.findU_discoverById(id);
			mv.addObject("u_discover", u_discover);
			
			List<ApiU_discover_spec> specs = this.apiU_discover_specService.getApiU_discover_specsBydiscover_Id(request.getParameter("id"));
			mv.addObject("specs", specs);
		}
		
		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(0);
		
		mv.addObject("cates", cates);
		
		return mv;
	}
	
	@RequestMapping("create-service-discover")
	public ModelAndView create_service_discover(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("/discover/create-discover-service");

		if(request.getParameter("id") != null){
			Integer id = Integer.valueOf(request.getParameter("id"));
			ApiU_discover u_discover = apiU_discoverService.findU_discoverById(id);
			mv.addObject("u_discover", u_discover);
			
			List<ApiU_discover_spec> specs = this.apiU_discover_specService.getApiU_discover_specsBydiscover_Id(request.getParameter("id"));
			mv.addObject("specs", specs);
		}
		
		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(1);
		
		mv.addObject("cates", cates);
		
		return mv;
	}
	/**
	 * 保存新发现活动
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("u_activity_discover-add")
	public void u_activity_discover_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String cateId = request.getParameter("cateId");
		String headImg = request.getParameter("head_img");
		String vedio = request.getParameter("vedio");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String description = request.getParameter("description");
		String[] chargenames = request.getParameterValues("chargenames");
		String[] chargefees = request.getParameterValues("chargefees");
		String[] chargelimits = request.getParameterValues("chargelimits");
		String regioninfo = request.getParameter("regioninfo");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		
		String discoverId = request.getParameter("id");
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		Object webuser = request.getSession().getAttribute("webuser");
		
		if (webuser == null) {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "尚未登录");
			return;
		}
		
		ApiTmember apiTmember = (ApiTmember) webuser;
		
		if (discoverId.equalsIgnoreCase("")) {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();
				
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(apiTmember.getCid().toString());
				apiU_discover.setType(0);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setLatitude(latitude);
				apiU_discover.setLongitude(longitude);
				
				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i=0; i<chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();
					
					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(BigDecimal.valueOf(Double.valueOf(chargefees[i])));
					
					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);
					
					apiU_discover_spec.setSort(i+1);
					apiU_discover_spec.setType(0);
					
					specs.add(apiU_discover_spec);
				}
				
				this.apiU_discoverService.U_discoverInsertionWithSpec(apiU_discover, specs);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		} else {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();
				
				apiU_discover.setId(Integer.valueOf(discoverId));
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(apiTmember.getCid().toString());
				apiU_discover.setType(0);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setLatitude(latitude);
				apiU_discover.setLongitude(longitude);
				
				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i=0; i<chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();
					
					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(BigDecimal.valueOf(Double.valueOf(chargefees[i])));
					
					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);
					
					apiU_discover_spec.setSort(i+1);
					apiU_discover_spec.setType(0);
					
					specs.add(apiU_discover_spec);
				}
				
				this.apiU_discoverService.U_discoverUpdateWithSpec(apiU_discover, specs);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "修改成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		}
		
		
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	private boolean validateAddActivityDiscover(HttpServletRequest request) {
		String cateId = request.getParameter("cateId");
		String vedio = request.getParameter("vedio");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String[] chargenames = request.getParameterValues("chargenames");
		String[] chargefees = request.getParameterValues("chargefees");
		String[] chargelimits = request.getParameterValues("chargelimits");
		
		String[] regioninfo = request.getParameterValues("regioninfo");
		
		if (cateId.equalsIgnoreCase("")) {
			return false;
		}
		
		if (vedio.equalsIgnoreCase("")) {
			return false;
		}
		
		if (title.equalsIgnoreCase("")) {
			return false;
		}
		
		if (address.equalsIgnoreCase("")) {
			return false;
		}
		
		/*if (beginTime.equalsIgnoreCase("")) {
			return false;
		}
		
		if (endTime.equalsIgnoreCase("")) {
			return false;
		}*/
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			if (sdf.parse(beginTime).after(sdf.parse(endTime))) {
				return false;
			}
			
			if (chargenames == null) {
				return false;
			} else {
				for (int i=0; i<chargenames.length; i++) {
					if (chargenames[i].length() == 0) {
						return false;
					}
					
					if (Float.valueOf(chargefees[i]) < 0) {
						return false;
					}
					
					if (Integer.valueOf(chargelimits[i]) < 0) {
						return false;
					}
				}
			}
		} catch (Exception e) {
			return false;
		}

		return true;
	}
	
	/**
	 * 保存新发现供应
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "u_service_discover-add")
	public void u_service_discover_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String cateId = request.getParameter("cateId");
		String headImg = request.getParameter("head_img");
		String vedio = request.getParameter("vedio");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String description = request.getParameter("description");
		String[] chargenames = request.getParameterValues("chargenames");
		String[] chargefees = request.getParameterValues("chargefees");
		String[] chargelimits = request.getParameterValues("chargelimits");
		String isAdd = request.getParameter("is_add");
		String regioninfo = request.getParameter("regioninfo");
		String discoverId = request.getParameter("id");
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		Object webuser = request.getSession().getAttribute("webuser");
		
		if (webuser == null) {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "尚未登录");
			return;
		}
		
		ApiTmember apiTmember = (ApiTmember) webuser;
		
		if (discoverId.equalsIgnoreCase("")) {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();
				
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setAdd_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(apiTmember.getCid().toString());
				apiU_discover.setType(1);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}
				
				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i=0; i<chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();
					
					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(BigDecimal.valueOf(Double.valueOf(chargefees[i])));
					
					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);
					
					apiU_discover_spec.setSort(i+1);
					apiU_discover_spec.setType(1);
					
					specs.add(apiU_discover_spec);
				}
				
				this.apiU_discoverService.U_discoverInsertionWithSpec(apiU_discover, specs);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		} else {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();
				
				apiU_discover.setId(Integer.valueOf(discoverId));
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(apiTmember.getCid().toString());
				apiU_discover.setType(1);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}
				
				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i=0; i<chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();
					
					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(BigDecimal.valueOf(Double.valueOf(chargefees[i])));
					
					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);
					
					apiU_discover_spec.setSort(i+1);
					apiU_discover_spec.setType(1);
					
					specs.add(apiU_discover_spec);
				}
				
				this.apiU_discoverService.U_discoverUpdateWithSpec(apiU_discover, specs);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "修改成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		}
		
		
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping("requirement_discover-list")
	public ModelAndView requirement_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("/discover/discover-requirement-list");
		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(2);
		Map conditions = new HashMap();
		
		String title = request.getParameter("activity_name");
		String status = request.getParameter("status");
		
		Object webuser = request.getSession().getAttribute("webuser");
		
		if (webuser == null) {
			return null;
		} else {
			ApiTmember apiTmember = (ApiTmember) webuser;
			conditions.put("user_id", apiTmember.getCid().toString());
		}
		
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", title);
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "2");
		
		List<Map<String,Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		mv.addObject("cates", cates);
		mv.addObject("u_discovers", u_discovers);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping("create_requirement_discover")
	public ModelAndView create_requirement_discover(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("/discover/create-discover-requirement");
		//
		if(request.getParameter("id") != null){
			Integer id = Integer.valueOf(request.getParameter("id"));
			ApiU_discover u_discover = apiU_discoverService.findU_discoverById(id);
			mv.addObject("u_discover", u_discover);
		}
		
		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(2);		
		mv.addObject("cates", cates);
		
		return mv;
	}
	
	/**
	 * 保存新发现需求
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "u_requirement_discover-add")
	public void u_requirement_discover_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String headImg = request.getParameter("head_img");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String description = request.getParameter("description");
		String isAdd = request.getParameter("is_add");
		String vedio = request.getParameter("vedio");
		String cateId = request.getParameter("cateId");
		String average_price = request.getParameter("average_price");
		String regioninfo = request.getParameter("regioninfo");
		
		String discoverId = request.getParameter("id");
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		Object webuser = request.getSession().getAttribute("webuser");
		
		if (webuser == null) {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "尚未登录");
			return;
		}
		
		ApiTmember apiTmember = (ApiTmember) webuser;
		
		if (discoverId.equalsIgnoreCase("")) {
			if (this.validateAddRequirementDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();
				apiU_discover.setAverage_price(new BigDecimal(average_price));
				apiU_discover.setAddress(address);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setVedio(vedio);
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setAdd_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(apiTmember.getCid().toString());
				apiU_discover.setType(2);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}
				System.out.println("yesyesye");
				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				
				this.apiU_discoverService.U_discoverInsertionWithSpec(apiU_discover, specs);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		} else {
			if (this.validateAddRequirementDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();
				apiU_discover.setAverage_price(new BigDecimal(average_price));
				apiU_discover.setId(Integer.valueOf(discoverId));
				apiU_discover.setAddress(address);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setVedio(vedio);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setAdd_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(apiTmember.getCid().toString());
				apiU_discover.setType(2);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}
				
				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				
				this.apiU_discoverService.U_discoverUpdateWithSpec(apiU_discover, specs);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "修改成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		}
		
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	private boolean validateAddRequirementDiscover(HttpServletRequest request) {
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		
		if (title.equalsIgnoreCase("")) {
			return false;
		}
		
		if (address.equalsIgnoreCase("")) {
			return false;
		}
		
		if (beginTime.equalsIgnoreCase("")) {
			return false;
		}
		
		if (endTime.equalsIgnoreCase("")) {
			return false;
		}
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			if (sdf.parse(beginTime).after(sdf.parse(endTime))) {
				return false;
			}
			
		} catch (Exception e) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * 新发现订单列表
	 * @param request
	 * @param response
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "order-activity-list")
	public ModelAndView order_activity_list(HttpServletRequest request, HttpServletResponse response, PageInfo page)
			throws Exception {
		ModelAndView mv = new ModelAndView("/discover/new_find_activity_list");
		Map<String, Object> cond = new HashMap<String, Object>();
		ApiTmember apiTmember = (ApiTmember) request.getSession().getAttribute("webuser");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String ordertype = request.getParameter("ordertype");
		cond.put("ordertype", ordertype);
		cond.put("userId", apiTmember.getCid());
		cond.put("ddStatus", request.getParameter("ddStatus"));
		cond.put("orderNum", request.getParameter("orderNum"));
		cond.put("startTime", startTime);
		cond.put("endTime", endTime);
		cond.put("fpStatus", request.getParameter("fpStatus"));
		List<Map<String, String>> resultList = apiU_discoverService.selectByOrListPage(cond, page);
		mv.addObject("newfind_list", resultList);
		mv.addObject("pageInfo", page);
		return mv;
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "discover-delete")
	public void u_discover_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("discover_id");

		// delete
		int ret_i = apiU_discoverService.deleteU_discover(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}
